#!/bin/bash

# 简化的提交命令 - 类似于 npm run commit
# 直接调用交互式提交工具

cd "$(dirname "$0")/.."
chmod +x scripts/interactive-commit.sh
exec bash scripts/interactive-commit.sh

# 交互式 Git 提交脚本
# 支持 Conventional Commits 规范

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 检查是否有待提交的更改
echo -e "${BLUE}🔍 检查待提交的更改...${NC}"
if ! git diff --cached --quiet; then
    echo -e "${GREEN}✅ 发现待提交的更改${NC}"
    echo ""
    git diff --cached --stat
    echo ""
else
    echo -e "${YELLOW}⚠️  没有待提交的更改，请先添加文件到暂存区${NC}"
    echo ""
    echo -e "${CYAN}💡 提示：${NC}"
    echo "  git add .              # 添加所有更改"
    echo "  git add <file>         # 添加特定文件"
    echo ""
    exit 1
fi

echo -e "${PURPLE}📝 请选择提交类型：${NC}"
echo ""
echo "  1. ${GREEN}feat${NC}     ✨ 新功能"
echo "  2. ${RED}fix${NC}      🐛 问题修复"
echo "  3. ${BLUE}docs${NC}     📚 文档更新"
echo "  4. ${YELLOW}style${NC}    🎨 代码格式（不影响运行的变动）"
echo "  5. ${PURPLE}refactor${NC} ♻️  重构（既不是新增功能，也不是修复bug的代码变动）"
echo "  6. ${CYAN}perf${NC}     ⚡ 性能优化"
echo "  7. ${GREEN}test${NC}     🧪 增加测试"
echo "  8. ${YELLOW}chore${NC}    ⚙️  构建过程或辅助工具的变动"
echo "  9. ${BLUE}ci${NC}       🔧 CI配置"
echo " 10. ${PURPLE}build${NC}    📦 构建系统"
echo " 11. ${RED}revert${NC}   ◀️  回滚"
echo ""

# 读取用户选择
while true; do
    read -p "请输入选择 (1-11): " choice
    case $choice in
        1) commit_type="feat"; emoji="✨"; break;;
        2) commit_type="fix"; emoji="🐛"; break;;
        3) commit_type="docs"; emoji="📚"; break;;
        4) commit_type="style"; emoji="🎨"; break;;
        5) commit_type="refactor"; emoji="♻️"; break;;
        6) commit_type="perf"; emoji="⚡"; break;;
        7) commit_type="test"; emoji="🧪"; break;;
        8) commit_type="chore"; emoji="⚙️"; break;;
        9) commit_type="ci"; emoji="🔧"; break;;
        10) commit_type="build"; emoji="📦"; break;;
        11) commit_type="revert"; emoji="◀️"; break;;
        *) echo -e "${RED}❌ 无效选择，请输入 1-11${NC}";;
    esac
done

echo ""
echo -e "${GREEN}✅ 已选择: ${commit_type} ${emoji}${NC}"
echo ""

# 询问作用域 (可选)
echo -e "${CYAN}🎯 请输入作用域 (可选，回车跳过):${NC}"
echo -e "${YELLOW}   例如: auth, ui, api, docs, config 等${NC}"
read -p "作用域: " scope

# 询问提交描述
echo ""
echo -e "${CYAN}📝 请输入提交描述:${NC}"
echo -e "${YELLOW}   简洁明了地描述这次更改的内容${NC}"
while true; do
    read -p "描述: " description
    if [[ -n "$description" ]]; then
        break
    else
        echo -e "${RED}❌ 描述不能为空，请重新输入${NC}"
    fi
done

# 询问详细描述 (可选)
echo ""
echo -e "${CYAN}📋 请输入详细描述 (可选，回车跳过):${NC}"
echo -e "${YELLOW}   可以包含多行，用于解释更改的原因、影响等${NC}"
echo -e "${YELLOW}   输入完成后按 Ctrl+D 结束输入${NC}"
echo ""

detailed_description=""
while IFS= read -r line; do
    if [[ -n "$detailed_description" ]]; then
        detailed_description="${detailed_description}\n${line}"
    else
        detailed_description="$line"
    fi
done

# 构建提交信息
if [[ -n "$scope" ]]; then
    commit_msg="${commit_type}(${scope}): ${description}"
else
    commit_msg="${commit_type}: ${description}"
fi

if [[ -n "$detailed_description" ]]; then
    commit_msg="${commit_msg}\n\n${detailed_description}"
fi

# 显示最终的提交信息
echo ""
echo -e "${PURPLE}📋 最终提交信息预览:${NC}"
echo "=====================================/"
echo -e "$commit_msg"
echo "=====================================/"
echo ""

# 确认提交
echo -e "${CYAN}❓ 确认提交吗？ (y/N)${NC}"
read -p "确认: " confirm

case $confirm in
    [Yy]|[Yy][Ee][Ss])
        echo ""
        echo -e "${BLUE}🚀 正在提交...${NC}"
        echo ""
        
        # 执行提交
        if echo -e "$commit_msg" | git commit -F -; then
            echo ""
            echo -e "${GREEN}✅ 提交成功！ ${emoji}${NC}"
            echo ""
            
            # 显示最新的提交
            echo -e "${CYAN}📝 最新提交:${NC}"
            git log --oneline -1
            echo ""
            
            # 询问是否推送
            echo -e "${CYAN}❓ 是否推送到远程仓库？ (y/N)${NC}"
            read -p "推送: " push_confirm
            case $push_confirm in
                [Yy]|[Yy][Ee][Ss])
                    echo ""
                    echo -e "${BLUE}📡 正在推送...${NC}"
                    if git push; then
                        echo -e "${GREEN}✅ 推送成功！${NC}"
                    else
                        echo -e "${RED}❌ 推送失败${NC}"
                    fi
                    ;;
                *)
                    echo -e "${YELLOW}ℹ️  跳过推送，稍后可使用 'git push' 手动推送${NC}"
                    ;;
            esac
        else
            echo -e "${RED}❌ 提交失败${NC}"
            exit 1
        fi
        ;;
    *)
        echo -e "${YELLOW}❌ 取消提交${NC}"
        exit 0
        ;;
esac